﻿using System;
using COM.DTO;
using NHibernate;
using COM.Logging;
using NHibernate.Criterion;

namespace DAL.DAO
{
    public class UserDAO
    {
        public static void Add(User User)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(User);
                    transaction.Commit();
                }
            }
        }
        public static void Update(User User)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(User);
                    transaction.Commit();
                }
            }
        }

        public static void Remove(User User)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(User);
                    transaction.Commit();
                }
            }
        }

        public static User GetUserById(String UserId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<User>(UserId);
        }
        public static User GetUserByUsername(string Username)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var user = session
                    .CreateCriteria(typeof(User))
                    .Add(Restrictions.Eq("Username", Username))
                    .UniqueResult<User>();
                return user;
            }

        }

    }
}
